<template>
  <div class="h-full flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <NCard :title="$t('page.dataplatform.cleanRule')" :bordered="false" size="small"
      class="sm:flex-1-hidden card-wrapper">
      <template #header-extra>
        <TableHeaderOperation v-model:columns="columnChecks" :disabled-delete="checkedRowKeys.length === 0"
          :loading="loading" @add="handleAdd" @delete="handleBatchDelete" @refresh="getData" />
      </template>
      <NDataTable v-model:checked-row-keys="checkedRowKeys" :columns="columns" :data="dataSource" size="small"
        :flex-height="!appStore.isMobile" :scroll-x="962" :loading="loading" remote :row-key="row => row.id"
        :pagination="mobilePagination" class="sm:h-full" />
      <CleanRuleOperateDrawer v-model:visible="drawerVisible" :operate-type="operateType" :row-data="editingData"
        @submitted="getData" />
    </NCard>
  </div>
</template>

<script setup lang="tsx">
import { NButton, NPopconfirm, NSpace, NTag } from 'naive-ui';
import { fetchCleanRuleList, deleteCleanRule } from '@/service/api';
import { useAppStore } from '@/store/modules/app';
import { useTable, useTableOperate } from '@/hooks/common/table';
import CleanRuleOperateDrawer from './modules/clean-rule-operate-drawer.vue';

const appStore = useAppStore();

const { columns, columnChecks, data, getData, loading, mobilePagination } = useTable({
  apiFn: fetchCleanRuleList,
  apiParams: {
    pageNum: 1,
    pageSize: 10
  },
  columns: () => [
    {
      type: 'selection',
      align: 'center',
      width: 48
    },
    {
      key: 'id',
      title: 'ID',
      align: 'center',
      width: 80
    },
    {
      key: 'name',
      title: '规则名称',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'code',
      title: '规则编码',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'dimension',
      title: '质量维度',
      align: 'center',
      width: 120,
      render: row => {
        const dimensionMap: Record<string, { label: string; type: string }> = {
          ACCURACY: { label: '准确性', type: 'success' },
          COMPLETENESS: { label: '完整性', type: 'info' },
          CONSISTENCY: { label: '一致性', type: 'warning' },
          UNIQUENESS: { label: '唯一性', type: 'primary' },
          VALIDITY: { label: '有效性', type: 'error' },
          TIMELINESS: { label: '及时性', type: 'default' }
        };
        const dim = dimensionMap[row.dimension] || { label: row.dimension, type: 'default' };
        return <NTag type={dim.type as any}>{dim.label}</NTag>;
      }
    },
    {
      key: 'cleanType',
      title: '清洗类型',
      align: 'center',
      width: 120
    },
    {
      key: 'priority',
      title: '优先级',
      align: 'center',
      width: 80
    },
    {
      key: 'status',
      title: '状态',
      align: 'center',
      width: 80,
      render: row => {
        return <NTag type={row.status === 1 ? 'success' : 'error'}>{row.status === 1 ? '启用' : '禁用'}</NTag>;
      }
    },
    {
      key: 'createTime',
      title: '创建时间',
      align: 'center',
      width: 180
    },
    {
      key: 'operate',
      title: '操作',
      align: 'center',
      width: 130,
      fixed: 'right',
      render: row => {
        return (
          <NSpace justify={'center'}>
            <NButton size={'small'} type="primary" onClick={() => handleEdit(row.id)}>
              编辑
            </NButton>
            <NPopconfirm onPositiveClick={() => handleDelete(row.id)}>
              {{
                default: () => '确认删除',
                trigger: () => (
                  <NButton size={'small'} type="error">
                    删除
                  </NButton>
                )
              }}
            </NPopconfirm>
          </NSpace>
        );
      }
    }
  ]
});

const { drawerVisible, operateType, editingData, handleAdd, handleEdit, checkedRowKeys, onBatchDeleted, onDeleted } =
  useTableOperate(data, getData);

async function handleBatchDelete() {
  await deleteCleanRule(checkedRowKeys.value);
  onBatchDeleted();
}

async function handleDelete(id: number) {
  await deleteCleanRule([id]);
  onDeleted();
}

const dataSource = data;
</script>

<style scoped></style>
